package com.tz.crm.dao.impl;

import com.tz.crm.dao.PaymentDao;
import com.tz.crm.entity.Payment;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 使用 @Repository 将 DAO 类声明为 Bean
 */
@Repository
@Transactional
public class PaymentDaoImpl extends BaseDaoImpl<Payment, String> implements PaymentDao {

    @Override
    @SuppressWarnings("unchecked")
    public List<Payment> getByContractId(String id) {
        String hql = "from Payment p where p.contract.id = :id order by p.payOffDate asc";
        return getSession().createQuery(hql).setParameter("id", id).list();
    }

    @Override
    public double getSumPaidMoney(int awayMonth) {
        String hql = "select sum(p.amount) from Payment p where createDate between :myFromDate and :myToDate and paid=true";
        Calendar firstDay = Calendar.getInstance();
        Calendar lastDay = Calendar.getInstance();
        firstDay.set(firstDay.get(Calendar.YEAR), firstDay.get(Calendar.MONTH) - awayMonth, 1);
        lastDay.set(lastDay.get(Calendar.YEAR), lastDay.get(Calendar.MONTH) - awayMonth + 1, 0);
        Query query = getSession().createQuery(hql);
        query.setParameter("myFromDate", firstDay.getTime());
        query.setParameter("myToDate", lastDay.getTime());
        if (query.uniqueResult() == null) {
            return 0.0;
        }
        return Double.valueOf(query.uniqueResult() + "");
    }

    @Override
    public double getSumUnpaidMoney(int awayMonth) {
        String hql = "select sum(p.amount) from Payment p where createDate between :myFromDate and :myToDate and paid=false";
        Calendar firstDay = Calendar.getInstance();
        Calendar lastDay = Calendar.getInstance();
        firstDay.set(firstDay.get(Calendar.YEAR), firstDay.get(Calendar.MONTH) - awayMonth, 1);
        lastDay.set(lastDay.get(Calendar.YEAR), lastDay.get(Calendar.MONTH) - awayMonth + 1, 0);
        Query query = getSession().createQuery(hql);
        query.setParameter("myFromDate", firstDay.getTime());
        query.setParameter("myToDate", lastDay.getTime());
        if (query.uniqueResult() == null) {
            return 0.0;
        }
        return Double.valueOf(query.uniqueResult() + "");
    }

    @Override
    public List<Payment> search(Payment payment) {
        String hql="select p from Payment p join p.contract c join c.signUser s where 1=1 and p.paid="+payment.isPaid();
        List<Object> objs=new ArrayList<>();
        if(null != payment.getPayName()){
            hql+=" and p.payName=?";
            objs.add(payment.getPayName());
        }
        if(StringUtils.isNotBlank(payment.getContract().getInNum().trim())){
            hql+=" and c.inNum=?";
            objs.add(payment.getContract().getInNum());
        }
        if(StringUtils.isNotBlank(payment.getContract().getCustomerName().trim())){
            hql+=" and c.customerName=?";
            objs.add(payment.getContract().getCustomerName());
        }
        if(null != payment.getContract().getSignUser()){
            hql+=" and s.id=?";
            objs.add(payment.getContract().getSignUser().getId());
        }
        Query<Payment> query = getSession().createQuery(hql, Payment.class);
        //如果集合存在,就发送该参数
        if(objs!=null && objs.size()>0){
            for (int i = 0; i < objs.size(); i++) {
                query.setParameter(i, objs.get(i));
            }
        }
        return query.list();
    }
}
